package com.woniuxy.joylifeshop.controller;

import com.alibaba.fastjson.JSON;
import com.woniuxy.common.entity.ShopMerchantAccount;
import com.woniuxy.joylifeshop.service.MerchantAccountService;
import com.woniuxy.joylifeshop.vo.ViewObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class MerchantAccountController {

    @Autowired
    MerchantAccountService merchantAccountService;

    /**
    * @Author SooYoo
    * @Description
    * @Param
    * @Return
    **/
    @RequestMapping("/editBalance")
    public ViewObject editBalance(@RequestBody String information){
        ViewObject viewObject = new ViewObject();

        try {
            //找到对象
            ShopMerchantAccount shopMerchantAccount = JSON.parseObject(information, ShopMerchantAccount.class);
            //验证最大值
            ShopMerchantAccount shopMerchantAccountReality = merchantAccountService.readMerchantAccountByPrimaryId(shopMerchantAccount);
            if(shopMerchantAccountReality.getBalance()<shopMerchantAccount.getBalance()){

                viewObject.setData(null);
                viewObject.setCount(1);
                viewObject.setCode(0);
                viewObject.setMsg("数额过大错误");

                return viewObject;
            }
            //验证负值
            if(shopMerchantAccount.getBalance()<=0){

                viewObject.setData(null);
                viewObject.setCount(1);
                viewObject.setCode(0);
                viewObject.setMsg("数额不能为负值或者0");

                return viewObject;
            }
            //修改数据库
            merchantAccountService.modifyByWithdraw(shopMerchantAccount);
            viewObject.setData(null);
            viewObject.setCount(1);
            viewObject.setCode(0);
            viewObject.setMsg("提现成功");

            return viewObject;
        } catch (Exception e) {
            e.printStackTrace();

            viewObject.setData(null);
            viewObject.setCount(1);
            viewObject.setCode(0);
            viewObject.setMsg("其他错误");

            return viewObject;
        }

    }






}
